package com.starsea.book.ui.newpage.viewmodel

import android.app.Application
import androidx.collection.arrayMapOf
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.starsea.book.base.BaseViewModel
import com.starsea.book.constant.AppConst
import com.starsea.book.help.http.newCallResponseBody
import com.starsea.book.help.http.okHttpClient
import com.starsea.book.help.http.postJson
import com.starsea.book.help.http.text
import com.starsea.book.ui.newpage.entity.AccountEntity
import com.starsea.book.ui.newpage.entity.BaseEntity
import com.starsea.book.ui.newpage.entity.WalletDetailsEntity
import com.starsea.book.ui.newpage.entity.WalletInfoEntity
import com.starsea.book.ui.newpage.entity.WalletWithDrawEntity
import com.starsea.book.ui.newpage.utils.gsonList
import com.starsea.book.ui.newpage.utils.gsonNormal
import com.starsea.book.utils.LogUtils
import com.starsea.book.utils.toastOnUi
import okhttp3.RequestBody.Companion.toRequestBody
import kotlin.collections.set

class WalletViewModel(application: Application) : BaseViewModel(application) {

    val walletInfoResult = MutableLiveData<BaseEntity<WalletInfoEntity>>()
    val walletDetailsResult = MutableLiveData<BaseEntity<WalletDetailsEntity>>()
    val withdrawListResult = MutableLiveData<BaseEntity<MutableList<WalletWithDrawEntity>>>()
    val accountListResult = MutableLiveData<BaseEntity<MutableList<AccountEntity>>>()
    val coinExchangeListResult = MutableLiveData<BaseEntity<MutableList<WalletWithDrawEntity>>>()
    val coinExchangeResultResult = MutableLiveData<BaseEntity<String?>>()
    val addAccountResult = MutableLiveData<BaseEntity<String?>>()
    val editAccountResult = MutableLiveData<BaseEntity<String?>>()
    val deleteAccountResult = MutableLiveData<BaseEntity<String?>>()
    val withdrawResult = MutableLiveData<BaseEntity<String?>>()
    fun getWalletInfo() {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/customer/wallet"))
            }.text().let {

                LogUtils.e(":>>>", ">>.${it}")
                val entity: BaseEntity<WalletInfoEntity> =
                    it.gsonNormal(WalletInfoEntity::class.java)
                walletInfoResult.postValue(entity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun getWalletDetails(walletType: Int, page: Int) {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/customer/walletList?walletType=${walletType}&pageNo=$page&pageSize=10"))

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val entity: BaseEntity<WalletDetailsEntity> =
                    it.gsonNormal(WalletDetailsEntity::class.java)
                walletDetailsResult.postValue(entity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun getWithdrawList() {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/asset/withdrawList"))

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val entity: BaseEntity<MutableList<WalletWithDrawEntity>> =
                    it.gsonList(WalletWithDrawEntity::class.java)
                withdrawListResult.postValue(entity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun getAccountList() {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/account/withdrawAccountList"))

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val entity: BaseEntity<MutableList<AccountEntity>> =
                    it.gsonList(AccountEntity::class.java)
                accountListResult.postValue(entity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun getCoinExchangeList() {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/asset/goldExchangeList"))

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val entity: BaseEntity<MutableList<WalletWithDrawEntity>> =
                    it.gsonList(WalletWithDrawEntity::class.java)
                it.gsonList(WalletWithDrawEntity::class.java)
                coinExchangeListResult.postValue(entity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun goldExchange(ruleId: String) {
        val map: MutableMap<String, Any> = arrayMapOf()
        map["ruleId"] = ruleId
        val json = Gson().toJson(map)
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/asset/goldExchange"))
                    .postJson(json)

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val baseEntity = BaseEntity<String?>()
                val objects = JsonParser.parseString(it).asJsonObject
                baseEntity.status = objects.get("status").asInt
                baseEntity.msg = objects.get("msg").asString
                coinExchangeResultResult.postValue(baseEntity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun addAccount(accountNumber: String, realName: String, isDefault: Int) {
        val map: MutableMap<String, Any> = arrayMapOf()
        map["accountNumber"] = accountNumber
        map["realName"] = realName
        map["type"] = 1
        map["isDefault"] = isDefault

        val json = Gson().toJson(map)
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/account/addWithdrawAccount"))
                    .postJson(json)

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val baseEntity = BaseEntity<String?>()
                val objects = JsonParser.parseString(it).asJsonObject
                baseEntity.status = objects.get("status").asInt
                baseEntity.msg = objects.get("msg").asString
                addAccountResult.postValue(baseEntity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun editAccount(accountId: String, accountNumber: String, realName: String, isDefault: Int) {
        val map: MutableMap<String, Any> = arrayMapOf()
        map["accountId"] = accountId
        map["accountNumber"] = accountNumber
        map["realName"] = realName
        map["type"] = 1
        map["isDefault"] = isDefault

        val json = Gson().toJson(map)
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/account/updWithdrawAccount"))
                    .postJson(json)

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val baseEntity = BaseEntity<String?>()
                val objects = JsonParser.parseString(it).asJsonObject
                baseEntity.status = objects.get("status").asInt
                baseEntity.msg = objects.get("msg").asString
                editAccountResult.postValue(baseEntity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun deleteAccount(accountId: String) {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/account/delWithdrawAccount?accountId=${accountId}"))
                    .post("".toRequestBody())

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val baseEntity = BaseEntity<String?>()
                val objects = JsonParser.parseString(it).asJsonObject
                baseEntity.status = objects.get("status").asInt
                baseEntity.msg = objects.get("msg").asString
                deleteAccountResult.postValue(baseEntity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }


    fun withdraw(ruleId: String) {
        val map: MutableMap<String, Any> = arrayMapOf()
        map["ruleId"] = ruleId

        val json = Gson().toJson(map)
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/asset/withdraw"))
                    .postJson(json)

            }.text().let {
                LogUtils.e(":>>>", ">>.${it}")
                val baseEntity = BaseEntity<String?>()
                val objects = JsonParser.parseString(it).asJsonObject
                baseEntity.status = objects.get("status").asInt
                baseEntity.msg = objects.get("msg").asString
                withdrawResult.postValue(baseEntity)

            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

}
